const { query } = require("../logger")
const User = require("../models/user")

const userController = {
  // 获取用户信息
  getUserInfo: async function (req, res, next) {
    try {
      let userData = await User.all()
      res.json({
        code: 200,
        message: "操作成功",
        data: userData,
      })
    } catch (e) {
      res.json({
        code: 0,
        message: "操作失败",
        data: e,
      })
    }
  },
  // 新增用户信息
  insrtUserInfo: async function (req, res, next) {
    try {
      console.log("请求参数", req.query)
      let userData = await User.all()
      console.log('用户信息', userData)
      let query = req.query
      // 没有填完全信息
      if (!query.name || !query.phone || !query.address) {
        res.json({
          code: 0,
          message: "用户信息没有填完全",
          data: '',
        })
        return
      }
      // 验证用户用户名字是否重复
      console.log('22', userData.some(item => item.name == query.name))
      if (userData.some(item => item.name == query.name)) {
        res.json({
          code: 0,
          message: "添加的用户名是重复的请重新填写",
          data: '',
        })
        return
      }

      // 地址验证
      //.....

      // 验证手机号码格式
      let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
      if (!phoneReg.test(query.phone)) {
        res.json({
          code: 0,
          message: "手机号码格式不对",
          data: '',
        })
        return
      }
      let insertUSer = await User.insert(query)
      res.json({
        code: 200,
        message: "新增用户信息成功",
        data: "",
      })
    } catch (e) {
      res.json({
        code: 0,
        message: "操作失败",
        data: e,
      })
    }
  },
  // 更改用户信息
  updateUserInfo: async function (req, res, next) {
    try {
      let id = Number.parseInt(req.query.id)
      await User.all()
    } catch (e) {
      res.json({
        code: 0,
        message: "操作失败",
        data: e,
      })
    }
  },
}

module.exports = userController
